import { setGlobal } from '@swda/utils'
import { passwordMapParams as Params } from '@swda/configs/params/index.js'
//-----------------------------------------------------------------------------
// Scene_Password
//
// 密码界面
function Scene_Password() {
    this.initialize.apply(this, arguments);
}

Scene_Password.prototype = Object.create(Scene_Base.prototype);
Scene_Password.prototype.constructor = Scene_Password;

Scene_Password.prototype.initialize = function() {
    Scene_Base.prototype.initialize.call(this);
    this.clearResult();
    this._map = this.genRandomMap();        // 获取密码表
    this._passwordLength = 3;               // 密码长度，原版为：3
    this.genNewPassword();                  // 生成新密码（序号+答案）
    this._inputPassword = 0;    
    this._inputCount = 0;
};

Scene_Password.prototype.resultSwitchId = function() {
    return 298;
};

Scene_Password.prototype.clearResult = function() {
    $gameSwitches.setValue(this.resultSwitchId(), false);
};

Scene_Password.prototype.makeResult = function(result) {
    $gameSwitches.setValue(this.resultSwitchId(), result);
};

Scene_Password.prototype.genNewPassword = function() {
    this._randomIndex = this.genRandomIndex();
    this._answerPassword = this.caculateAnswer();
};

// 【临时】生成随机密码表（图块颜色）！！！！！！！！！！！！！！！！！！！！！！！！！
Scene_Password.prototype.genRandomMap = function() {
    return Params.C;
    //return [1,2,3,2,1,3,4,3,2,1,2,3,4,5];
    let randomMap = [];
    const mapLength = 53;   //密码表共53个图块
    for (let i = 0; i < mapLength; i++){
        randomMap.push(Math.floor(Math.random() * 5) + 1);//生成1~5随机编号。
    }
    return randomMap;
};

// 生成随机序号（图块编号）：根据密码长度随机且不重复地生成：[0, this._map.length-1]范围内的整数。
Scene_Password.prototype.genRandomIndex = function() {
    let randomIndex = [];
    for (let i = 0; i < this._passwordLength; i++){
        //randomIndex.push(Math.floor(Math.random() * this._map.length));
        let randIndexTemp = -1;//初始值
        // 如果序号列表中已存在该随机数，则不断生成，直到生成一个不重复的随机数。
        do {
            randIndexTemp = Math.floor(Math.random() * this._map.length);
        } while (randomIndex.contains(randIndexTemp));
        randomIndex.push(randIndexTemp);
    }
    return randomIndex;
};

Scene_Password.prototype.caculateAnswer = function() {
    let answerPassword = 0;
    for (let i = 0; i < this._passwordLength; i++){
        answerPassword *= 10;
        answerPassword += this._map[this._randomIndex[i]]
    }
    return answerPassword;
};

Scene_Password.prototype.create = function() {
    Scene_Base.prototype.create.call(this);
    this.createBackground();
    this.createWindowLayer();
    this.createCommandWindow();
    this.createMapCursor();
};

// 生成光标
Scene_Password.prototype.createMapCursor = function() {
    this._mapCursor = new Sprite();
    this._mapCursor.bitmap = ImageManager.loadBitmap('img/system/UI/', 'PasswordCursor');
    this._mapCursor.visible = true;
    this.refreshMapCursor();
    this.addChild(this._mapCursor);
};

Scene_Password.prototype.start = function() {
    Scene_Base.prototype.start.call(this);
    this.startFadeIn(this.fadeSpeed());
};

Scene_Password.prototype.update = function() {
    if (!this.isBusy()) {
        this._commandWindow.open();
    }
    Scene_Base.prototype.update.call(this);
};

Scene_Password.prototype.isBusy = function() {
    return this._commandWindow.isClosing() || Scene_Base.prototype.isBusy.call(this);
};

Scene_Password.prototype.createBackground = function() {
    this._backSprite = new Sprite(ImageManager.loadBitmap('img/system/UI/', 'MenuBg_Password'));
    this.addChild(this._backSprite);
};

Scene_Password.prototype.createCommandWindow = function() {
    this._commandWindow = new Window_PasswordCommand(923, 47);
    this._commandWindow.setHandler('ok',     this.onPasswordOk.bind(this));
    this._commandWindow.setHandler('cancel',     this.onPasswordCancel.bind(this));
    this.addWindow(this._commandWindow);
};

Scene_Password.prototype.onPasswordOk = function() {
    this._inputPassword = this._inputPassword * 10 + this._commandWindow.index()+1;
    this._inputCount += 1;
    this._commandWindow.activate();
    this.comparePassword();
    this.refreshMapCursor();
};

Scene_Password.prototype.onPasswordCancel = function() {
    this.startFadeOut(this.fadeSpeed());
    this.popScene();
};

Scene_Password.prototype.refreshMapCursor = function() {
    this._mapCursor.x = Params.X[this._randomIndex[this._inputCount]];
    this._mapCursor.y = Params.Y[this._randomIndex[this._inputCount]];
    /*
    let color = "";
    switch (Params.C[this._inputCount]) {
    case 1:
        color = "green";
        break;
    case 2:
        color = "白";
        break;
    case 3:
        color = "蓝";
        break;
    case 4:
        color = "黄";
        break;
    case 5:
        color = "红";
        break;
    }
    console.log(color)
    */
};

Scene_Password.prototype.comparePassword = function(){
    if (this._inputCount !== this._passwordLength)
        return;
    if(this._inputPassword === this._answerPassword){
        this.rightPassword();
    } else {
        this.wrongPassword();
    }
        
};

Scene_Password.prototype.rightPassword = function(){
    this.makeResult(true);
    this.fadeOutAll();
    this.popScene();
};

Scene_Password.prototype.wrongPassword = function(){
    SoundManager.playBuzzer();
    this.makeResult(false);
    // 错误即重来
    this.genNewPassword();
    this._inputPassword = 0;
    this._inputCount = 0;
    // 错误即退出
    // this.fadeOutAll();
    // this.popScene();
};

Scene_Password.prototype.commandCancel = function(){
    this.makeResult(false);
    this.fadeOutAll();
    this.popScene();
};


setGlobal('Scene_Password', Scene_Password)